﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using permit.App_Code.Entity;
using permit.App_Code.DataAccess;

namespace permit
{
    public partial class DepartmentInfo : System.Web.UI.Page
    {
        string sturts;
        int depID;

        DepartmentAccess depAcc = new DepartmentAccess();

        protected void Page_Load(object sender, EventArgs e)
        {
            btnDelet.Attributes.Add("onclick", "javascript:return(confirm('是否要删除该部门！'))");
            if (!Page.IsPostBack)
            {
                AssginAccess assAcc = new AssginAccess();
                if (!assAcc.IsDepartmentAssgin(((UserTable)Session["UserTable"]).USER_ID) || ((UserTable)Session["UserTable"]).DEP_PRINT == 1)
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=Department");
                }
                sturts = Request.QueryString["Sturt"];

                if (sturts == "Edit")
                {
                    depID = Convert.ToInt32(Request.QueryString["id"]);
                    if (depID > 0)
                    {
                        DepartmentTable dep = depAcc.GetDepartment(depID);

                        txtDepDescr.Text = dep.DEP_DESCRIPTION;
                        txtDepName.Text = dep.DEP_NAME;
                        rblDepPrint.SelectedValue = dep.DEP_PRINT.ToString();
                        txtAreaCode.Text = dep.DEP_AREACODE;
                        hdDepName.Value = dep.DEP_NAME;
                        hdAreaCode.Value = dep.DEP_AREACODE;
                        
                    }

                    btnAdd.Visible = false;
                    btnEdit.Visible = true;
                }
                if (sturts == "Add")
                {
                    btnAdd.Visible = true;
                    btnEdit.Visible = false;
                    btnDelet.Visible = false;
                }
                if (sturts == null)
                {
                    btnDelet.Visible = false;
                }

            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("DepartmentManager.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtAreaCode.Text.Length < 6)
            {
                Page.RegisterStartupScript("this", "<script>alert('请输入6位编码！')</script>");
                return;
            }
            if (txtDepName.Text != hdDepName.Value || txtAreaCode.Text != hdAreaCode.Value)
            {
                if (depAcc.IsDepartment(txtDepName.Text, txtAreaCode.Text))
                {
                    Page.RegisterStartupScript("this1", "<script>alert('该部门已存在或者编码重复！')</script>");
                    return;
                }
            }
            DepartmentTable dep = new DepartmentTable();

            dep.DEP_NAME = txtDepName.Text;
            dep.DEP_DESCRIPTION = txtDepDescr.Text;
            dep.DEP_PRINT = rblDepPrint.SelectedIndex;
            dep.DEP_AREACODE = txtAreaCode.Text;

            dep.DEP_ID = Convert.ToInt32(Request.QueryString["id"]);

            int count = depAcc.UpdateDepartment(dep);
            if (count > 0)
            {
                Response.Redirect("DepartmentInfo.aspx");
            }
            else
            {
                Page.RegisterStartupScript("this","<script>alert('修改失败！请查看网络是否连接！')</script>");
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtAreaCode.Text.Length < 6)
            {
                Page.RegisterStartupScript("this", "<script>alert('请输入6位编码！')</script>");
                return;
            }
            if (depAcc.IsDepartment(txtDepName.Text, txtAreaCode.Text))
            {
                Page.RegisterStartupScript("this", "<script>alert('该部门已存在或者编码重复！')</script>");
                return;
            }
            DepartmentTable dep = new DepartmentTable();

            dep.DEP_NAME = txtDepName.Text;
            dep.DEP_DESCRIPTION = txtDepDescr.Text;
            dep.DEP_PRINT = rblDepPrint.SelectedIndex;
            dep.DEP_AREACODE = txtAreaCode.Text;

            int count = depAcc.InsertDepartment(dep);

            if (count > 0)
            {
                Response.Redirect("DepartmentInfo.aspx");
            }
            else
            {
                Page.RegisterStartupScript("this1", "<script>alert('添加数据失败！请查看网络是否连接！')</script>");
            }
        }

        protected void btnDelet_Click(object sender, EventArgs e)
        {
            int count = depAcc.DeleteDepartment(Convert.ToInt32(Request.QueryString["id"]));

            if (count > 0)
            {
                Response.Redirect("DepartmentInfo.aspx");
            }
            else
            {
                Page.RegisterStartupScript("this2", "<script>alert('删除数据失败！请查看网络是否连接！')</script>");
            }

        }

        protected void gridDeparment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[e.Row.Cells.Count - 1].Text == Convert.ToString(0))
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Text = "是";
            }
            if (e.Row.Cells[e.Row.Cells.Count - 1].Text == Convert.ToString(1))
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Text = "否";
            }
        }
    }
}
